/*
 * Imports in H2 database.
 */
package org.passwordstore.util;

import java.sql.Connection;
import java.util.ArrayList;
import org.passwordstore.Resource;

/**
 *
 * @author thotheolh
 */
public class ImportH2DB {

    private Connection conn = null;
    private int importMode = 0; // Default '0' which is inclusive import.
    private SQLManager sql;
    private ArrayList<Password> faultyImportArray;
    // Flags
    public static int INCLUSIVE_IMPORT = 0; // Incrementally include target database to local database.
    public static int OVERWRITE_IMPORT = 1; // Entirely overwrites local database using target database.

    public ImportH2DB(Connection conn, int importMode) {
        setConn(conn);
    }

    public ImportH2DB(String JDBCUrl, String username, char[] pwd, int importMode) {
        conn = new DBManager(JDBCUrl, username, new String(pwd)).getConnection();
    }

    public boolean doImport() {
        sql = new SQLManager();
        sql.setConn(conn);
        setFaultyImportArray(new ArrayList<Password>());
        if (getImportMode() == 1) {
            // Overwrite mode
            return exclusiveImport();
        } else {
            //Inclusive mode
            return inclusiveImport();
        }
    }

    private boolean inclusiveImport() {
        resetFaultyImportArray();
        int err = 0;
        ArrayList<Password> importPwdArray = sql.getAllPasswords();
        for (int i = 0; i < importPwdArray.size(); i++) {
            if (!Resource.getInstance().getSql().addPassword(importPwdArray.get(i))) {
                this.addToFaultyImport(importPwdArray.get(i));
                err = err++;
            }
        }
        if (err != 0) {
            return false;
        }
        return false;
    }

    private boolean exclusiveImport() {
        resetFaultyImportArray();
        int err = 0;
        if (Resource.getInstance().getSql().wipePasswordTable()) {
            ArrayList<Password> importPwdArray = sql.getAllPasswords();
            for (int i = 0; i < importPwdArray.size(); i++) {
                if (!Resource.getInstance().getSql().addPassword(importPwdArray.get(i))) {
                    this.addToFaultyImport(importPwdArray.get(i));
                    err = err++;
                }
            }
            if (err != 0) {
                return false;
            }
        }
        return false;
    }

    /**
     * @return the conn
     */
    public Connection getConn() {
        return conn;
    }

    /**
     * @param conn the conn to set
     */
    public void setConn(Connection conn) {
        this.conn = conn;
    }

    /**
     * @return the importMode
     */
    public int getImportMode() {
        return importMode;
    }

    /**
     * @param importMode the importMode to set
     */
    public void setImportMode(int importMode) {
        this.importMode = importMode;
    }

    /**
     * Gets list of faulty imports.
     * For exclusive imports, can be useful import is problematic.
     * For inclusive imports, can be used to check duplicate entries.
     * @return the faultyImportArray
     */
    public ArrayList<Password> getFaultyImportArray() {
        return faultyImportArray;
    }

    /**
     * Adds another faulty import to list.
     * @param badPass 
     */
    public void addToFaultyImport(Password badPass) {
        faultyImportArray.add(badPass);
    }

    /**
     * Clears the faulty import list.
     */
    public void resetFaultyImportArray() {
        faultyImportArray.clear();
    }

    /**
     * @param faultyImportArray the faultyImportArray to set
     */
    private void setFaultyImportArray(ArrayList<Password> faultyImportArray) {
        this.faultyImportArray = faultyImportArray;
    }
}
